<?php
/**
 * DataAccessDatabase
 * 
 * Database connection class.  
 * 
 * @author Gabriel PREDA
 * @copyright  Copyright (c) 2008-2009 Acceleron Team
 * @version    $Id: DataAccessDatabase.php 25 2008-11-28 20:20:19Z gabriel.preda $
 */

/**
 * Database local wrapper
 * @internal error code 271
 */
class DataAccessDatabase extends PDO
{

/**
 * Database driver to use
 *
 * @var string
 */
private $Type = 'mysql';
/**
 * Host to connect to
 *
 * @var string
 */
private $Host = 'localhost';

/**
 * Username to use in connection
 *
 * @var string
 */
private $Username = 'acceleron';
/**
 * Enter description here...
 *
 * @var string
 */
private $Password = 'acceleron';
/**
 * Name of the database you want to use
 *
 * @var string
 */
private $Database = 'acceleron';
/**
 * Driver specific options
 *
 * @var array
 */
private $Options = array();

/**
 * PDO specific string used for connection
 *
 * @var string
 */
private $DSN;

/**
 * Db Connection
 */
public function __construct($Db, $DriverOptions)
{

	$this->StoreVariables($Db, $DriverOptions);

	try
	{
		parent::__construct($this->DSN, $this->Username, $this->Password, $this->Options);
	}
	catch (PDOException $e)
	{
		
		error_log(serialize($e), E_USER_WARNING);
		throw new DataAccessDatabaseException(_('Unable to connect to database.'), 271);
	}
}


/**
 * Store variables on current object
 *
 * @param array $Db
 * @param array $DriverOptions
 */
private function StoreVariables($Db, $DriverOptions)
{
	if (isset($Db['Type']))		$this->Type 	= $Db['Type'];
	if (isset($Db['Host']))		$this->Host 	= $Db['Host'];
	if (isset($Db['Username']))	$this->Username = $Db['Username'];
	if (isset($Db['Password']))	$this->Password = $Db['Password'];
	if (isset($Db['Database']))	$this->Database = $Db['Database'];
	
	if (isset($DriverOptions) && is_array($DriverOptions) && (count($DriverOptions) >= 1))
	{
		$this->Options = $DriverOptions;
	}

	$this->DSN = $this->Type . ':host=' . $this->Host . ';dbname=' . $this->Database;
}

}

/**
 * Database exceptions
 * @internal error code 271
 */
 class DataAccessDatabaseException extends AcceleronException
{
}
